package com.toune.dltools.view.tab

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.viewpager.widget.ViewPager
import com.flyco.tablayout.CommonTabLayout
import com.flyco.tablayout.SegmentTabLayout
import com.flyco.tablayout.SlidingTabLayout
import com.flyco.tablayout.listener.CustomTabEntity
import com.toune.dltools.R
import kotlinx.android.synthetic.main.layout_dl_tab.view.*

class DLTabVpView(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {
    var TAB_POSITION = TabPosition.BOTTOM

    var topTab: SlidingTabLayout? = null
    var vp: ViewPager? = null
    var bottomTab: CommonTabLayout? = null
    private var titleStrs: List<String>? = null

    init {
        setSlidingTabStyle(attrs)
    }


    private fun setSlidingTabStyle(attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.DLTabVpView)
        val position = typedArray.getInt(R.styleable.DLTabVpView_position, 0)
        var layout = R.layout.layout_dl_tab
        var view = View.inflate(context, layout, null)
        topTab = view.findViewById(R.id.topTab)
        bottomTab = view.findViewById(R.id.bottomTab)
        vp = view.findViewById(R.id.vp)
        var fl = view.findViewById<FrameLayout>(R.id.fl)
        when (position) {
            0 -> {
                TAB_POSITION = TabPosition.TOP
                view.topLv.visibility= View.VISIBLE
                view.bottomLv.visibility = View.GONE
            }
            1 -> {
                TAB_POSITION = TabPosition.BOTTOM
                view.topLv.visibility= View.GONE
                view.bottomLv.visibility = View.VISIBLE
            }
        }
        typedArray.recycle()
        addView(view)
    }

    fun setTitles(titles: List<String>) {
        titleStrs = titles
    }

    fun setTitles(titles: List<String>, selectImgIds: List<Int>, unSelectImgIds: List<Int>) {
        titleStrs = titles
        this.selectImgIds = selectImgIds
        this.unSelectImgIds = unSelectImgIds
    }

    val vpFragments: MutableList<Fragment> = ArrayList()
    var selectImgIds: List<Int>? = null
    var unSelectImgIds: List<Int>? = null
    fun setFragments(fragments: MutableList<Fragment>) {
        vpFragments.clear()
        vpFragments.addAll(fragments)
    }

    var mTabEntities = ArrayList<CustomTabEntity>()
    fun setAdapter(fa:FragmentActivity,fragments: MutableList<Fragment>) {
        when (TAB_POSITION) {
            TabPosition.TOP -> {
                topTab!!.setViewPager(vp,titleStrs!!.toTypedArray(),fa,fragments as java.util.ArrayList<Fragment>)
            }
            TabPosition.BOTTOM -> {
                for (i in titleStrs!!.indices) {
                    mTabEntities.add(
                        TabEntity(
                            titleStrs!![i],
                            selectImgIds!![i],
                            unSelectImgIds!![i]
                        )
                    )
                }
                bottomTab!!.setTabData(mTabEntities,fa,R.id.fl,fragments as java.util.ArrayList<Fragment>)
            }
        }
    }
}